<?php

Yii::import('zii.widgets.grid.CDataColumn');

/**
 * Class YDatetimeColumn
 *
 * @author Hua Yang <htmlcook@gmail.com>
 * @package backend.widgets
 * @since 1.0
 */
class YDatetimeColumn extends CDataColumn
{
	public $nullDisplay;

	public function init()
	{
		parent::init();
		$this->htmlOptions['class'] = $this->type . '-column';
		$this->headerHtmlOptions['class'] = $this->type . '-column';
		$this->footerHtmlOptions['class'] = $this->type . '-column';
	}

	/**
	 * @see CGridColumn::renderDataCellContent()
	 */
	protected function renderDataCellContent($row, $data)
	{
		$data = $this->grid->dataProvider->data[$row];

		if ($this->value !== null) {
			$value = $this->evaluateExpression($this->value, array('data' => $data, 'row' => $row));
		} elseif ($this->name !== null) {
			$value = CHtml::value($data, $this->name);
		} else {
			$value = null;
		}

		if ($value) {
			$timeDiff = time() - $value;
			if ($timeDiff > 0 && $timeDiff < YUtil::DAY_IN_SECONDS) {
				$content = YUtil::humanTimeDiff($value);
			} else {
				$content = $this->grid->formatter->format($value, $this->type);
			}
			$formatter = $this->grid->getFormatter();
			echo CHtml::tag('abbr', array('title' => $formatter->formatDatetime($value)), $content);
		} else {
			echo $this->nullDisplay;
		}
	}
}